import React from 'react'

import {WeaLocaleProvider, WeaTools,WeaCheckbox,WeaHelpfulTip} from 'ecCom'
const getLabel = WeaLocaleProvider.getLabel;
import AddExtend from "./addExtend";
import SettingExtend from "./settingExtend";
import {message} from "antd";
import { WeaPortalShare } from 'weaPortalPublic';

export default class SettingProxy extends React.Component{

    onGetColsAfterHandler = (columns)=>{
        const {javaInterface,data,layoutType} = this.props
        const {itemsForSetting} = data
        if(columns.length == 2){
            columns[0].width = '30%'
            columns[1].width = '40%'
        }

        let temp = {
            title: getLabel(89, '显示'),
            dataIndex: 'isShow',
            key: 'isShow',
            width: '15%',
            render: (text,record)=>{
                return (
                    <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@9ylnkx`}
                        value={text}
                        onChange={(value)=>{
                            const item = itemsForSetting.find(function(item){
                                return item.id == record.id
                            })
                            item.setting.isShow = value

                            WeaTools.callApi(javaInterface.saveOrUpdateItem, 'POST', {"json":JSON.stringify({id:item.id,setting:JSON.stringify(item.setting)})}).then((result) => {
                                if (result.status == true) {
                                    this.props.onLoad({handlerType:'savaOrUpdateItem',id:item.id})
                                }
                            });
                        }}
                    />
                )
            }
        }
        let temp1 = {
            title: (
                <div>{getLabel(512999, '使用限制')} <WeaHelpfulTip ecId={`${this && this.props && this.props.ecId || ''}_WeaHelpfulTip@x2d3cj`} title={getLabel('391204','不设置代表所有人可见')}/></div>
            ),
            dataIndex: 'share',
            key: 'share',
            width: '15%',
            render: (text,record)=>{
                return (
                    <div>
                        <i style={{ cursor: 'pointer', fontSize: '16px', color: '#b2b2b2' }} className="icon-coms-Flow-setting" onClick={() => { this.share.onLoad({ element: record.elementId, app: record.id },getLabel(82752, '共享范围')+": "+record.title) }}/>
                        {
                            record.hasShareData == true ? (
                                <i style={{ color: '#52c133', 'margin-left':'10px' }} className="icon-coms-Selected" />
                            ) : null
                        }
                    </div>
                )
            }
        }
        if(layoutType == 'theme'){
            columns.splice(2,0,temp1,temp)
        }else{
            columns.splice(2,0,temp)
        }


    }

    onGetDataAfterHandler = (datas)=>{
        let items = this.props.data.itemsForSetting
        for(let i in datas){
            if(datas.hasOwnProperty(i)){
                datas[i].isShow = items[i].setting.isShow
                datas[i].elementId = this.props.data.id
                datas[i].share = items[i].setting.share
                datas[i].hasShareData = items[i].hasShareData
            }
        }

    }

    render(){
        const {Setting} = this.props
        return (
            <React.Fragment ecId={`${this && this.props && this.props.ecId || ''}_undefined@nx10dx`}>
                <WeaPortalShare ecId={`${this && this.props && this.props.ecId || ''}_WeaPortalShare@pf90jc`} ref={share => this.share = share} onCancelCallback={()=>{this.props.onLoad()}}/>
                <Setting ecId={`${this && this.props && this.props.ecId || ''}_Setting@sf7bw4`} ref='Setting' {...this.props} rewriteShareComponent={true} onGetColsAfterHandler={this.onGetColsAfterHandler} onGetDataAfterHandler={this.onGetDataAfterHandler} AddExtend={AddExtend} SettingExtend={SettingExtend}></Setting>
            </React.Fragment>
        )

    }

}